<?php

namespace Home\Controller;

use Think\Controller;

class CommentController extends Controller {
    function __destruct(){                                                                         // 析构方法  写入用户访问和操作日志
        require '/home/wwwroot/default/world/behavior.php';
    }
	// 发表评论
	public function releaseCom() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		if (null != $_POST ['content']) { // 判断是否有评论
			
			$comment = M ( 'comments' ); // 实例化comments
			$ideas = M ( 'ideas' ); // 实例化ideas
			$iid = $_REQUEST ['iid']; // 取iid
			$uid = session ( 'userUid' ); // session取uid
			$user = session ( 'userUsername' ); // session取用户名
			
			if ($ideas->where ( "iid=$iid" )->getField ( 'iscomment' ) == 1) { // 判断是否允许评论
				$Hint->myerror ( "此创意不允许评论！", "1" );
				die ();
			}
			
			$date ['iid'] = $iid;
			$date ['uid'] = $uid;
			$date ['cuser'] = $user;
			$date ['content'] = $_POST ['content'];
			$date ['cdate'] = date ( "Y-m-d H:i:s" ); // 生成发布日期
			
			$result = $comment->where ( "uid = $uid" )->add ( $date ); // 添加数据库
			if ($result) {
				$Hint->mysuccee ( "发表成功！", "1" );
				// $this->display ( 'Home@Idea:details' );
				die ();
			} else {
				$Hint->myerror ( "发表失败！", "1" );
				die ();
			}
		} else {
			$Hint->myerror ( "发表失败！", "1" );
			die ();
		}
	}
	
	// 评论列表
	public function showlist() {
		$showmodel = M ( 'comments' ); // 实例化comments
		$users = M ( 'users' ); // 实例化users
		
		$iid = $_REQUEST ['iid'];
		$number = $showmodel->where ( "iid=$iid" )->count (); // 有多少评论
		$uid = $showmodel->where ( "iid=$iid" )->field ( 'uid' )->select (); // 取出uid的值
		
		$show = $showmodel->where ( "iid=$iid" )->order ( 'cdate asc' )->select (); // 查询评论
		for($i = 0; $i < $number; $i ++) {
			$cdate = $show [$i] ['cdate']; // 取出发布时间
			$sub = substr ( $cdate, 11, 2 ); // 将发布时间的时截取出来
			$uiid = $uid [$i] ['uid']; // 取出uid的值
			$show [$i] ['photo'] = $users->where ( "uid = $uiid" )->getField ( "photo" ); // 查询符合条件的值
			if ($sub < 12) { // 判断是否小于12，如果小于12，就是上午，否则就是下午
				$show [$i] ['time'] = "上午";
				$show [$i] ['num'] = $i; // 计数
			} else {
				$show [$i] ['time'] = "下午";
				$show [$i] ['num'] = $i;
			}
		}
		
		$this->assign ( 'number', $number );
		$this->assign ( 'show', $show );
	}
}